﻿using AutoMapper;
using B.S.Business.Domain;
using B.S.Business.ErrorCode;
using B.S.Business.Infacetrue;
using B.S.Business.Write.API.Applications.Command.SupplierCommand;
using DAL;
using MediatR;
using Yitter.IdGenerator;

namespace B.S.Business.Write.API.Applications.CommandHandler.SupperlierCommandHandler
{
    public class SupperlierAddCommandHandler : IRequestHandler<SupplierAddCommand, ApiResult<object>>
    {
        private readonly IBaseRepository<SupplierApp> supplierRepo;
        private readonly IMapper mapper;
        private readonly DBHelper helper;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="supplierRepo"></param>
        /// <param name="mapper"></param>
        public SupperlierAddCommandHandler(IBaseRepository<SupplierApp> supplierRepo, IMapper mapper, DBHelper helper)
        {
            this.supplierRepo = supplierRepo;
            this.mapper = mapper;
            this.helper = helper;
        }
        /// <summary>
        /// 供应商添加
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiResult<object>> Handle(SupplierAddCommand request, CancellationToken cancellationToken)
        {
            ApiResult<object> result = new ApiResult<object>();

            result.Msg = ApiCode.添加成功.ToString();
            result.Code = ApiCode.添加成功;

            var sql = $"insert into SupplierApp values('{request.SupplierNumber = YitIdHelper.NextId().ToString()}','{request.SupplierName}','{request.OrderAmount="合作供应商"}','{request.SupplierPhone}','{request.ChuanZhen}','{request.Email}','{request.SupplierNamer = "张三"}','{request.SupplierAdd}','{request.SupplierTime}','{request.IsDeleted}')";

            var ss = helper.NonQuery(sql);

            if (ss>0)
            {
                result.Msg = ApiCode.添加成功.ToString();
                result.Code = ApiCode.添加成功;
                result.Data = ss;
                return Task.FromResult(result);
            }
            else
            {
                result.Msg = ApiCode.添加失败.ToString();
                result.Code = ApiCode.添加失败;
                return Task.FromResult(result);
            }
        }
    }
}
